//
//  MHMomentVideoView.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHMomentVideoView.h"
#import "MHMomentItemViewModel.h"
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerControlView.h>
#import "MHRedPacketBtn.h"
@interface MHMomentVideoView ()

/// coverView




@property (nonatomic, strong) ZFPlayerControlView *controlView;

/// playBtn


/// viewModel
@property (nonatomic, readwrite, strong) MHMomentItemViewModel *viewModel;


@end


@implementation MHMomentVideoView

- (void)bindViewModel:(MHMomentItemViewModel *)viewModel{
    self.viewModel = viewModel;
    [self.coverView yy_setImageWithURL:viewModel.moment.videoInfo.coverURL placeholder:MHWebImagePlaceholder()];
    self.redPacketBtn.hidden = (self.viewModel.moment.redInfo == nil) ;
    [self.redPacketBtn configRedPacketWithRedPacketWithRedInfo:self.viewModel.moment.redInfo redPacketPositon:RedPackePostionCirle];
}


- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
    }
    return _controlView;
}

+ (instancetype)videoView{
    
    return [[self alloc] init];
}




- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        // 初始化
        [self _setup];
        
        // 创建自控制器
        [self _setupSubViews];
        self.userInteractionEnabled = NO;
        
        // 布局子控件
        
    }
    return self;
}

#pragma mark - 初始化
- (void)_setup
{
    
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    [self _makeSubViewsConstraints];
}
#pragma mark - 初始化子空间
- (void)_setupSubViews{
    /// avatarView
    YYAnimatedImageView *coverView = [[YYAnimatedImageView alloc] init];
//    coverView.userInteractionEnabled = YES;
    self.coverView = coverView;
    [self addSubview:coverView];
    
    self.coverView.contentMode = UIViewContentModeScaleAspectFill;
    self.coverView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.coverView.clipsToBounds = YES;
    
    /// 播放按钮
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [playBtn setImage:MHImageNamed(@"icon_play_w") forState:UIControlStateNormal];
    [playBtn setImage:MHImageNamed(@"icon_play_w") forState:UIControlStateHighlighted];
    self.playBtn = playBtn;
//    [playBtn addTarget:self action:@selector(playClick:) forControlEvents:UIControlEventTouchUpInside];

    [coverView addSubview:playBtn];
    
    
    MHRedPacketBtn *redBtn = [MHRedPacketBtn buttonWithType:UIButtonTypeCustom];
    [redBtn setImage:MHImageNamed(@"ic_video_package_wk") forState:UIControlStateNormal];
    [redBtn setImage:MHImageNamed(@"ic_video_package_wk") forState:UIControlStateHighlighted];
    self.redPacketBtn = redBtn;
    
    [coverView addSubview:redBtn];
}


//- (void)playClick:(UIButton *)sender {
//    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
//    [MHMomentHelper hideAllPopViewWithAnimated:YES];
//    //    KSMediaPlayerManager *playerManager = [[KSMediaPlayerManager alloc] init];
//    //    ZFIJKPlayerManager *playerManager = [[ZFIJKPlayerManager alloc] init];
//    /// 播放器相关
//    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self];
//    self.player.shouldAutorotate = YES;
//    self.player.controlView = self.controlView;
//    self.player.stopWhileNotVisible = YES;
//    
//    
//    [[[MHHTTPService sharedInstance] updateRecentViewCountWithVideoId:[self.viewModel.moment.videoInfo.videoId intValue] recentId:self.viewModel.moment.autoId] subscribeNext:^(id x) {
//        
//    }];
//    @weakify(self)
//    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
//        @strongify(self)
//        if (!isFullScreen) {
//            [self.player stop];
////            [self.player stopCurrentPlayingCell];
//            self.player = nil;
//        }
//    };
//    self.player.playerDidToEnd = ^(id  _Nonnull asset) {
//        @strongify(self)
//        if (self.player.isFullScreen) {
////            [self.player enterFullScreen:NO animated:YES];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.player.orientationObserver.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self.player stop];
//                [self.player stopCurrentPlayingCell];
//            });
//        } else {
//            [self.player stop];
//        }
//    };
//    
//    playerManager.assetURL = self.viewModel.moment.videoInfo.videoURL;
//    [self.controlView showTitle:@"" coverURLString:self.viewModel.moment.videoInfo.videoURL.absoluteString fullScreenMode:ZFFullScreenModePortrait];
//    [self.player enterFullScreen:YES animated:YES];
//}

#pragma mark - 布局子控件
- (void)_makeSubViewsConstraints{
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    
    [self.redPacketBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-10);
        make.right.equalTo(self).offset(-10);
    }];
}

-(void)dealloc
{
//    [self.player stop];
}


@end
